﻿// -----------------------------------------------------------------------
// <copyright file="PhieuXetNghiemChiTietDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class PhieuXetNghiemChiTietDataAccess
    {
        public bool InsertPhieuXetNghiemChiTiet(PhieuXetNghiemChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuXetNghiemChiTiet_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, obj.MaXetNghiem);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "KetQua", DbType.String, obj.KetQua);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertPhieuXetNghiemChiTiet", ex.Message); throw ex; }
        }

        public bool EditPhieuXetNghiemChiTiet(PhieuXetNghiemChiTietEntity obj)
        {
            try
            {
                string sql = "spPhieuXetNghiemChiTiet_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, obj.MaXetNghiem);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, obj.MaChiTieu);
                db.AddInParameter(cmd, "KetQua", DbType.String, obj.KetQua);
                db.AddInParameter(cmd, "DonGia", DbType.Double, obj.DonGia);
                db.AddInParameter(cmd, "DaThuTien", DbType.Int32, obj.DaThuTien);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdatePhieuXetNghiemChiTiet", ex.Message); throw ex; }
        }

        public bool DeletePhieuXetNghiemChiTiet(string maXetNghiem, string maChiTieu)
        {
            try
            {
                string sql = "spPhieuXetNghiemChiTiet_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, maXetNghiem);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuXetNghiemChiTiet", ex.Message); throw ex; }
        }
        
        public bool DeletePhieuXetNghiemChiTiet_ByMaPhieuXetNghiem(string maXetNghiem)
        {
            try
            {
                string sql = "[dbo].[spPhieuXetNghiemChiTiet_DeleteByMaXetNghiem]";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, maXetNghiem);

                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeletePhieuXetNghiemChiTiet", ex.Message); throw ex; }
        }

        public PhieuXetNghiemChiTietEntity GetPhieuXetNghiemChiTiet(string maXetNghiem, string maChiTieu)
        {
            try
            {
                PhieuXetNghiemChiTietEntity obj = null;
                string sql = "spPhieuXetNghiemChiTiet_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, maXetNghiem);
                db.AddInParameter(cmd, "MaChiTieu", DbType.String, maChiTieu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuXetNghiemChiTietEntity();
                        obj.MaXetNghiem = reader["MaXetNghiem"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuXetNghiemChiTiet", ex.Message); throw ex; }
        }

        public List<PhieuXetNghiemChiTietEntity> GetPhieuXetNghiemChiTietList()
        {
            try
            {
                List<PhieuXetNghiemChiTietEntity> list = new List<PhieuXetNghiemChiTietEntity>();
                PhieuXetNghiemChiTietEntity obj = null;
                string sql = "spPhieuXetNghiemChiTiet_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuXetNghiemChiTietEntity();
                        obj.MaXetNghiem = reader["MaXetNghiem"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuXetNghiemChiTietList", ex.Message); throw ex; }
        }

        public List<PhieuXetNghiemChiTietEntity> GetPhieuXetNghiemChiTietByMaXetNghiemList(string maXetNghiem)
        {
            try
            {
                List<PhieuXetNghiemChiTietEntity> list = new List<PhieuXetNghiemChiTietEntity>();
                PhieuXetNghiemChiTietEntity obj = null;
                string sql = "spPhieuXetNghiemChiTiet_SelectByMaXetNghiem";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaXetNghiem", DbType.String, maXetNghiem);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new PhieuXetNghiemChiTietEntity();
                        obj.MaXetNghiem = reader["MaXetNghiem"].ToString();
                        obj.MaChiTieu = reader["MaChiTieu"].ToString();
                        obj.KetQua = reader["KetQua"].ToString();
                        obj.DonGia = String.IsNullOrEmpty(reader["DonGia"].ToString()) == true ? 0 : (double)reader["DonGia"];
                        obj.DaThuTien = String.IsNullOrEmpty(reader["DaThuTien"].ToString()) == true ? 0 : (Int32)reader["DaThuTien"];
                        obj.TenChiTieu = reader["TenChiTieu"].ToString();
                        obj.DonVi = reader["DonVi"].ToString();
                        obj.TriSoBinhThuong = reader["TriSoBinhThuong"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetPhieuXetNghiemChiTietByMaXetNghiemList", ex.Message); throw ex; }
        }
    }
}